home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
COMM
/
MSKRMSRC.ARJ
/
MSNTCP.H
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-24
|
15KB
|
463 lines
/* File MSNTCP.H
* Main include file for TCP/IP, as revised and modified for MS-DOS Kermit.
*
* Copyright (C) 1991, University of Waterloo.
* Copyright (C) 1991, Trustees of Columbia University in the
* City of New York. Permission is granted to any individual or
* institution to use, copy, or redistribute this software as long as
* it is not sold for profit and this copyright notice is retained.
*
* Original version created by Erick Engelke of the University of
* Waterloo, Waterloo, Ontario, Canada.
* Erick Engelke Erick@development.watstar.uwaterloo.ca
* Faculty of Engineering
* University of Waterloo (519) 885-1211 Ext. 2965
* 200 University Ave.,
* Waterloo, Ont., Canada
* N2L 3G1
* Adapted and modified for MS-DOS Kermit by Joe R. Doupnik,
* Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet,
* and by Frank da Cruz, Columbia University, fdc@watsun.cc.columbia.edu.
*
* Name resolution services were adapted from sources made available by
* the National Centre for Supercomputer Applications (NCSA) and Clarkson
* University.
*
* The C code is designed for the small memory model of Microsoft C versions
* 5.1 and 6.00A, with structures packed on one byte boundaries. No other
* options are expected.
*
* Last edit:
* 6 Sept 1991
*/
/* Kernel version (major major minor minor) */
#define WTCP_VER 0x0311
#define KERMIT
/*
* Typedefs and constants
*/
#ifndef byte
typedef unsigned char byte;
#endif /* byte */
#ifndef word
typedef unsigned int word;
#endif /* word */
#ifndef longword
typedef unsigned long longword;
#endif /* longword */
/*
#define DEBUG
*/
#ifndef NULL
#define NULL 0
#endif
#define TRUE 1
#define FALSE 0
#ifdef KERMIT
#define ETH_MSS 536 /* MSS for Ethernet */
#define tcp_MaxBufSize 1024 /* max bytes to buffer in a tcp socket */
#define udp_MaxBufSize 534 /* max bytes to buffer in a udp socket */
#define MAX_GATE_DATA 2
#define MAX_NAMESERVERS 3
#define MAX_COOKIES 0
#else
#define ETH_MSS 1500 /* MSS for Ethernet */
#define tcp_MaxBufSize 2048 /* maximum bytes to buffer in a socket */
#define udp_MaxBufSize 2048 /* max bytes to buffer in a udp socket */
#define MAX_GATE_DATA 12
#define MAX_NAMESERVERS 10
#define MAX_COOKIES 10
#endif
#define MAX_STRING 50
#define TICKS_SEC 18
#define PD_ETHER 1
#define PD_SLIP 6
/* These are Ethernet protocol numbers but are used for other things too */
#define UDP_PROTO 0x11
#define TCP_PROTO 0x06
#define ICMP_PROTO 0x01
#define TCP_MODE_BINARY 0 /* default mode */
#define TCP_MODE_ASCII 1
#define TCP_MODE_NAGLE 0 /* Nagle algorithm */
#define TCP_MODE_NONAGLE 4
#define UDP_MODE_CHK 0 /* default to having checksums */
#define UDP_MODE_NOCHK 2 /* turn off checksums */
typedef int (*procref)();
typedef byte eth_address[6];
/* The Ethernet header */
typedef struct {
eth_address destination;
eth_address source;
word type;
} eth_Header;
/* The Internet Header: */
typedef struct {
byte hdrlen_ver; /* both in one byte */
byte tos;
word length;
word identification;
word frag;
byte ttl;
byte proto;
word checksum;
longword source;
longword destination;
} in_Header;
#define in_GetVersion(ip) ((ip)->hdrlen_ver >> 4)
#define in_GetHdrlen(ip) ((ip)->hdrlen_ver & 0xf) /* 32 bit word size */
#define in_GetHdrlenBytes(ip) (in_GetHdrlen(ip) << 2) /* 8 bit byte size */
#define in_GetTos(ip) ((ip)->tos)
#define in_GetTTL(ip) ((ip)->ttl)
#define in_GetProtocol(ip) ((ip)->proto)
typedef struct {
word srcPort;
word dstPort;
word length;
word checksum;
} udp_Header;
#define UDP_LENGTH (sizeof(udp_Header))
typedef struct {
word srcPort;
word dstPort;
longword seqnum;
longword acknum;
word flags;
word window;
word checksum;
word urgentPointer;
} tcp_Header;
#define tcp_FlagFIN 0x0001
#define tcp_FlagSYN 0x0002
#define tcp_FlagRST 0x0004
#define tcp_FlagPUSH 0x0008
#define tcp_FlagACK 0x0010
#define tcp_FlagURG 0x0020
#define tcp_FlagDO 0xF000
#define tcp_GetDataOffset(tp) (intel16((tp)->flags) >> 12)
/* The TCP/UDP Pseudo Header */
typedef struct {
longword src;
longword dst;
byte mbz;
byte protocol;
word length;
word checksum;
} tcp_PseudoHeader;
/*
* TCP states, from tcp manual.
* Note: close-wait state is bypassed by automatically closing a connection
* when a FIN is received. This is easy to undo.
*/
#define tcp_StateLISTEN 0 /* listening for connection */
#define tcp_StateSYNSENT 1 /* syn sent, active open */
#define tcp_StateSYNREC 2 /* syn received, synack+syn sent. */
#define tcp_StateESTAB 3 /* established */
#define tcp_StateFINWT1 4 /* sent FIN */
#define tcp_StateFINWT2 5 /* sent FIN, received FINACK */
#define tcp_StateCLOSWT 6 /* received FIN waiting for close */
#define tcp_StateCLOSING 7 /* sent FIN, received FIN (waiting for FINACK) */
#define tcp_StateLASTACK 8 /* fin received, finack+fin sent */
#define tcp_StateTIMEWT 9 /* dally after sending final FINACK */
#define tcp_StateCLOSEMSL 10
#define tcp_StateCLOSED 11 /* finack received */
/*
* UDP socket definition
*/
typedef struct udp_socket {
struct udp_socket *next;
word ip_type; /* always set to UDP_PROTO */
byte *err_msg; /* null when all is ok */
void (*usr_yield)();
word soc_mode; /* a logical OR of bits */
longword usertimer; /* ip_timer_set, ip_timer_timeout */
procref dataHandler;
eth_address hisethaddr; /* peer's ethernet address */
longword hisaddr; /* peer's internet address */
word hisport; /* peer's UDP port */
word myport;
int rdatalen; /* must be signed */
byte rdata[udp_MaxBufSize];
} udp_Socket;
/*
* TCP Socket definition
*/
typedef struct tcp_socket {
struct tcp_socket *next;
word ip_type; /* always set to TCP_PROTO */
byte *err_msg;
void (*usr_yield)();
word soc_mode; /* a logical OR of bits */
longword usertimer; /* ip_timer_set, ip_timer_timeout */
procref dataHandler; /* called with incoming data */
eth_address hisethaddr; /* ethernet address of peer */
longword hisaddr; /* internet address of peer */
word hisport; /* tcp ports for this connection */
word myport;
int rdatalen; /* must be signed */
byte rdata[tcp_MaxBufSize]; /* received data */
word rmaxdatalen; /* normally tcp_MaxBufSize */
word state; /* connection state */
longword acknum;
longword seqnum; /* data ack'd and sequence num */
long timeout; /* timeout, in milliseconds */
byte unhappy; /* flag, indicates retransmitting segt's */
word flags; /* tcp flags word for last packet sent */
word window; /* other guy's window */
int datalen; /* number of bytes of data to send */
/* must be signed */
byte cwindow; /* Van Jacobson's algorithm */
byte wwindow;
word vj_sa; /* VJ's alg, standard average */
word vj_sd; /* VJ's alg, standard deviation */
longword vj_last; /* last transmit time */
word rto;
byte karn_count; /* count of packets */
/* retransmission timeout proceedure, these are in PC clock ticks */
longword rtt_lasttran; /* last transmission time */
longword rtt_smooth; /* smoothed round trip time */
longword rtt_delay; /* delay for next transmission */
longword rtt_time; /* time of next transmission */
word mss;
byte data[tcp_MaxBufSize]; /* data to send */
} tcp_Socket;
/* sock_type used for socket io */
typedef union {
udp_Socket udp;
tcp_Socket tcp;
} sock_type;
/* similar to UNIX */
typedef struct sockaddr {
word s_type;
word s_port;
longword s_ip;
byte s_spares[6]; /* unused in TCP realm */
};
/*
* ARP definitions
*/
#define arp_TypeEther 0x100 /* ARP type of Ethernet address */
/* ARP style op codes */
#define ARP_REQUEST 0x0100
#define ARP_REPLY 0x0200
#define RARP_REQUEST 0x0300 /* RARP request */
#define RARP_REPLY 0x0400 /* RARP reply */
/* Arp header */
typedef struct {
word hwType;
word protType;
word hwProtAddrLen; /* hw and prot addr len */
word opcode;
eth_address srcEthAddr;
longword srcIPAddr;
eth_address dstEthAddr;
longword dstIPAddr;
} arp_Header;
/*
* socket macros
*/
/*
* sock_wait_established()
* - waits then aborts if timeout on s connection
* sock_wait_input()
* - waits for received input on s
* - may not be valid input for sock_Gets... check returned length
* sock_tick()
* - do tick and jump on abort
* sock_wait_closed();
* - discards all received data
*
* jump to sock_err with contents of *statusptr set to
* 1 on closed
* -1 on timeout
*
*/
#define sock_wait_established(s, seconds, fn, statusptr) \
if (ip_delay0(s, seconds, fn, statusptr)) goto sock_err;
#define sock_wait_input(s, seconds, fn , statusptr) \
if (ip_delay1(s, seconds, fn, statusptr)) goto sock_err;
#define sock_tick(s, statusptr) \
if (!tcp_tick(s)) { *statusptr = 1 ; goto sock_err; }
#define sock_wait_closed(s, seconds, fn, statusptr)\
if (ip_delay2(s, seconds, fn, statusptr)) goto sock_err;
longword resolve();
int ping(longword host, longword countnum);
longword chk_ping(longword host, longword *ptr);
int add_server(int *counter, int max, longword *array, longword value);
byte * rip(byte *s);
chk_socket(tcp_Socket *s);
byte * inet_ntoa(byte *s, longword x);
byte * psocket(tcp_Socket *s);
longword inet_addr(byte *s);
byte * sockerr(tcp_Socket *s);
byte * sockstate(tcp_Socket *s);
byte * getdomainname(byte *name, int length);
byte * setdomainname(byte *string);
sock_init(void);
/* s is a pointer to a udp or tcp socket */
sock_read(void *s, byte *dp, int len);
sock_fastread(void *s, byte *dp, int len);
sock_write(void *s, byte *dp, int len);
sock_fastwrite(void *s, byte *dp, int len);
sock_flush(void *s);
sock_flushnext(void *s);
sock_puts(void *s, byte *dp);
word sock_gets(void *s, byte *dp, int n);
sock_putc(void *s, byte c);
sock_getc(void *s);
word sock_dataready(void *s);
sock_close(void *s);
void sock_abort(void *s);
sock_printf(void *s, byte *format, ...);
sock_scanf(void *s, byte *format, ...);
sock_mode(void *s, word mode); /* see TCP_MODE_... */
db_write(byte *msg);
dbuginit();
/*
* TCP or UDP specific material, must be used for open's and listens, but
* sock calls are used for everything else.
*/
int udp_open(void *s, word lport, longword ina, word port,
int (*datahandler)());
int tcp_open(void *s, word lport, longword ina, word port,
int (*datahandler)());
int tcp_listen(void *s, word lport, longword ina, word port,
int (*datahandler)(), word timeout);
int tcp_established(void *s);
/* less general functions */
byte * rip(byte *s);
longword resolve(byte *name);
int isaddr(byte *text);
longword intel(longword x);
word intel16(word x);
/* timers */
void ip_timer_init(void * s, int delayseconds);
int ip_timer_expired(void * s);
/* tcp_init/tcp_shutdown, init/kill all tcp and lower services.
Call if sock_init is not used, else not recommended.
*/
int tcp_Init();
void tcp_shutdown();
int tcp_abort(tcp_Socket *s);
/* tcp_tick - called periodically by user application in sock_wait.
returns 0 when our socket closes
*/
int tcp_tick(void *s);
/* tcp_set_debug_state - set 1 or reset 0, development tool */
void tcp_set_debug_state(word x);
int tcp_cancel(void * ip);
int udp_cancel(void * ip);
longword aton(byte * string);
int ping(longword host, longword countnum);
longword chk_ping(longword host, longword *ptr);
void arp_register(longword use, longword instead_of);
int eth_init();
byte * eth_formatpacket(void *eth_address, word eth_type);
int eth_send(word len);
void eth_free(void *buf);
byte * th_arrived(word *type_ptr);
void eth_release();
void * eth_hardware(void * p);
/* bsd-similar stuff */
int sock_rbsize(void *s);
int sock_rbused(void *s);
int sock_rbleft(void *s);
int sock_tbsize(void *s);
int sock_tbused(void *s);
int sock_tbleft(void *s);
getpeername(tcp_Socket *s, void *dest, int *len);
getsockname(tcp_Socket *s, void *dest, int *len);
longword gethostid();
longword sethostid(longword ip);
chk_socket(tcp_Socket *s);
byte * inet_ntoa(byte *s, longword x);
byte * psocket(tcp_Socket *s);
longword inet_addr(byte *s);
byte * sockerr(tcp_Socket *s);
byte * sockstate(tcp_Socket *s);
getpeername(tcp_Socket *s, void *dest, int *len);
getsockname(tcp_Socket *s, void *dest, int *len);
longword gethostid();
longword sethostid(longword ip);
byte * getdomainname(byte *name, int length);
byte * setdomainname(byte *string);
byte * gethostname(byte *name, int length);
byte * sethostname(byte *string);
word ntohs(word a);
word htons(word a);
longword ntohl(longword x);
longword htonl(longword x);
longword realclock(void);
void * movmem(void *, void *, int);
void arp_register(longword use, longword instead_of);
int arp_resolve(longword ina, void *ethap);
void arp_init(void);
extern survivebootp;
extern word pktdevclass;
extern word mss;
extern word bootptimeout;
extern longword bootphost;
extern word bootpon;
extern longword my_ip_addr;
extern eth_address eth_addr;
extern eth_address eth_brdcast;
extern longword sin_mask;
extern word sock_delay;
/*extern byte *def_domain;*/
extern int last_nameserver;
extern word debug_on;
extern longword def_nameservers[MAX_NAMESERVERS];
extern byte *hostname;
/* user initialization file */
extern void (*usr_init)();